package org.jsrml.core.base.entity;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.jsrml.common.persistence.StringIdBaseEntity;
import org.jsrml.common.util.UUIDGenerator;
import org.jsrml.core.base.command.SaveRoleCo;

/**
 * 角色
 */
@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "ROLE")
@SuppressWarnings("serial")
public class Role extends StringIdBaseEntity {

	/**
	 * 名称
	 */
	@Column(name = "NAME", length = 128)
	private String name;

	/**
	 * 编码
	 */
	@Column(name = "CODE", length = 128)
	private String code;
	
	
	/**
	 * 描述
	 */
	@Column(name = "DESCR",columnDefinition = M.TEXT_COLUMN)
	private String descr;

	/**
	 * 拥有授权的功能
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = M.TABLE_PREFIX + "ROLE_RESOURCE", joinColumns = { @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "RESOURCE_ID", referencedColumnName = "ID") })
	private Set<Resource> resources;

	public void create(SaveRoleCo command) {
		setId(UUIDGenerator.getUUID());
		setName(command.getName());
		setCode(command.getCode());
		setDescr(command.getDescr());
	}

	public void modify(SaveRoleCo command) {
		setName(command.getName());
		setCode(command.getCode());
		setDescr(command.getDescr());
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}

	public Set<Resource> getResources() {
		return resources;
	}

	public void setResources(Set<Resource> resources) {
		this.resources = resources;
	}

}
